package com.aboo.design.builder;

import com.alibaba.fastjson2.JSON;
import lombok.Data;

/**
 * 构造器模式
 * 封装一个复杂对象构造过程，并允许按步骤构造。
 */
public class BuilderPattern {
    public static void main(String[] args) {
        StudentInfo studentInfo = new StudentInfo.Builder("Nemo", "6(14)")
                .setStudentNo("07")
                .setStudentAge(12)
                .setStudentAddress("丽岛路15号")
                .setStudentBirth("2012-03-22")
                .build();
        System.out.println(JSON.toJSONString(studentInfo));
    }
}

@Data
class StudentInfo {
    private String studentName;
    private String className;

    private String studentNo;
    private Integer studentAge;
    private String studentBirth;
    private String studentAddress;

    public static class Builder {
        private StudentInfo studentInfo;

        public Builder(String studentName, String className) {
            studentInfo = new StudentInfo();
            studentInfo.studentName = studentName;
            studentInfo.className = className;
        }

        public Builder setStudentNo(String studentNo) {
            studentInfo.studentNo = studentNo;
            return this;
        }

        public Builder setStudentAge(Integer studentAge) {
            studentInfo.studentAge = studentAge;
            return this;
        }

        public Builder setStudentBirth(String studentBirth) {
            studentInfo.studentBirth = studentBirth;
            return this;
        }

        public Builder setStudentAddress(String studentAddress) {
            studentInfo.studentAddress = studentAddress;
            return this;
        }

        public StudentInfo build() {
            return studentInfo;
        }
    }
}
